<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <span v-if="isEdit">
        <span @click="searchHistories.splice(0)">全部删除</span>&nbsp;
        <span @click="isEdit=false">完成</span>
      </span>
      <van-icon v-else name="delete" @click="isEdit=true" />
    </van-cell>
    <van-cell 
    v-for="(item,index) in searchHistories" 
    :key="index"  
    @click="historyClick(item,index)"
    :title="item">
      <van-icon  name="close" v-if="isEdit" />
    </van-cell>
  
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    // 子组件定义属性 接收历史记录
    searchHistories:{
      type:Array
    }
  },
  data () {
    return {
      isEdit:false //是否处于编辑状态
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 历史记录点击行为
    historyClick(val,idx) {
      // 判断是否处于编辑状态
      if(this.isEdit) {
        // 是 删除操作
        this.searchHistories.splice(idx,1)
      }else {
        // 否 将历史记录项放到输入框 显示搜索结果
        this.$emit('search',val)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>